package router

import (
	"admin-system-go/model"
	"admin-system-go/model/dto"
	"admin-system-go/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type RoleRouter struct {
	roleService service.RoleService
}

func NewRoleRouter() RoleRouter {
	return RoleRouter{roleService: service.GetRoleService()}
}

func (roleRouter RoleRouter) Register(r *gin.Engine) {
	r.GET(apiVersion+"/user/role/list", roleRouter.QueryRole())
	r.POST(apiVersion+"/user/role/create", roleRouter.createRole())
	r.POST(apiVersion+"/user/role/update", roleRouter.updateRole())
	r.POST(apiVersion+"/user/role/delete", roleRouter.deleteRole())
}

func (roleRouter RoleRouter) QueryRole() gin.HandlerFunc {
	return func(context *gin.Context) {
		var roleQueryDTO dto.RoleQueryDTO
		err := context.ShouldBind(&roleQueryDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, roleRouter.roleService.QueryRole(roleQueryDTO))
	}
}

func (roleRouter RoleRouter) createRole() gin.HandlerFunc {
	return func(context *gin.Context) {
		var roleBaseDTO dto.RoleBaseDTO
		err := context.ShouldBind(&roleBaseDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, roleRouter.roleService.CreateRole(roleBaseDTO))
	}
}

func (roleRouter RoleRouter) deleteRole() gin.HandlerFunc {
	return func(context *gin.Context) {
		var roleDelDTO dto.RoleDelDTO
		err := context.ShouldBind(&roleDelDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, roleRouter.roleService.DelRole(roleDelDTO))
	}
}

func (roleRouter RoleRouter) updateRole() gin.HandlerFunc {
	return func(context *gin.Context) {
		var roleUpdateDTO dto.RoleUpdateDTO
		err := context.ShouldBind(&roleUpdateDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, roleRouter.roleService.UpdateRole(roleUpdateDTO))
	}
}
